package com.xxgc.mybatis.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxgc.mybatis.dao.UserDao;
import com.xxgc.mybatis.po.User;
import com.xxgc.mybatis.po.extend.UserExtend;
import com.xxgc.mybatis.returnData.LoginInfoData;
import com.xxgc.mybatis.returnData.UserTimeData;
import com.xxgc.mybatis.service.UserService;
import com.xxgc.mybatis.util.MyBatisUtils;
import com.xxgc.mybatis.vo.RespBean;
import com.xxgc.mybatis.vo.RespBeanEnum;
import org.apache.ibatis.session.SqlSession;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {
    public RespBean userRegister(User user) {
        //获取sqlsession
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //获取userDao
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        //判断是否有用户注册
        User chickuser = mapper.chickUser(user.getUName());
        if(chickuser==null){
            System.out.println("注册");
            //添加用户信息
            int result = mapper.registerUser(user);
            //提交
            sqlSession.commit();
            if(result == 1){
                //关闭
                sqlSession.close();
                return RespBean.success(RespBeanEnum.REGISTERSUCCESS);
            }
        }else{
            //关闭
            sqlSession.close();
            return RespBean.error(RespBeanEnum.REGISTERERROR);
        }

        //关闭
        sqlSession.close();

        return RespBean.error(RespBeanEnum.REGISTERERROR);
    }

    public RespBean UserLogin(String uName, String uPassword) {

        //获取sqlsession
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //获取userDao
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user = mapper.loginUser(uName,uPassword);
        if(user!=null){
            //登录成功
            //记录登录时间
            int result = mapper.addLoginInfo(user.getUId());
            sqlSession.commit();
            if(result == 1){
                sqlSession.close();
                return RespBean.success(RespBeanEnum.LOGINSUCCESS);
            }else{

                sqlSession.rollback();
                sqlSession.close();
                return RespBean.error(RespBeanEnum.LOGINERROR);
            }

        }
        return RespBean.success(RespBeanEnum.LOGINSUCCESS);
    }


    public RespBean loginRescord() {
        //获取sqlsession
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //获取userDao
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        PageHelper.startPage(1,10);
        List<UserExtend> list =  mapper.selectAllLoginInfo();
        PageInfo<UserExtend> page = new PageInfo<UserExtend>(list);
        LoginInfoData loginInfoData = new LoginInfoData();
        loginInfoData.setUserExtends(page.getList());
        loginInfoData.setNum(page.getSize());

        return RespBean.success(loginInfoData);
    }

    public RespBean chickUserTime() throws ParseException {
        //获取sqlsession
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //获取userDao
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        //创建一个集合用来存储
        List<UserTimeData> list = new ArrayList();
        //确定时间 17:00

        //利用循环执行6
        //初始化时间
        String time = "2021-10-19 17:00:00";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(int i = 0;i<6;i++){
            //拿到开始的时间
            long start = simpleDateFormat.parse(time).getTime();
            String starts = simpleDateFormat.format(start);
            //每循环一次就+10分钟去查询用户信息
            //拿到结束的时间
            Long end = start+600000;
            String ends = simpleDateFormat.format(end);
            //查询数据库
            List<UserExtend> userExtends=  mapper.chickUserTime(starts,ends);
            //放入对象
            UserTimeData userTimeData = new UserTimeData();
            userTimeData.setStart(starts);
            userTimeData.setEnd(ends);
            userTimeData.setUserExtends(userExtends);

            list.add(userTimeData);
            //时间进行赋值
            time = simpleDateFormat.format(end);

        }

        return RespBean.success(list);
    }
}
